package com.ziyun.erp.common.utils;

import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author guizhao
 * @Description: 采购类
 * @date 2018-09-27 16:04
 */
public class PurchaseUtils {
    private static Logger logger = LoggerFactory.getLogger(PurchaseUtils.class);

    /**
     * 生成采购申请单号
     * @return PY + 2位门店ID + YYMMDD + 3位随机数
     */
    public static synchronized String producePurchaseApplyNo(String str,int sellerId){
        StringBuffer purchaseApplyNo = new StringBuffer(str);

        return AutoNo(sellerId, purchaseApplyNo);
    }
    /**
     * 生成采购申请单号
     * @return PY + 2位门店ID + YYMMDD + 3位随机数
     */
    public static synchronized String producePurchaseApplyNo(int sellerId){
        StringBuffer purchaseApplyNo = new StringBuffer("PY");

        return AutoNo(sellerId, purchaseApplyNo);
    }

    private static String AutoNo(int sellerId, StringBuffer purchaseApplyNo) {
        if(sellerId < 10){
            purchaseApplyNo.append(0).append(sellerId);
        }else{
            purchaseApplyNo.append(sellerId);
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        String formatStr =dateFormat.format(new Date());
        purchaseApplyNo.append(formatStr);

        //随机数
        int one = (int)(Math.random()*10);
        int two = (int)(Math.random()*10);
        int three = (int)(Math.random()*10);

        purchaseApplyNo.append(one).append(two).append(three);
        logger.info("生成采购申请单号："+purchaseApplyNo.toString());
        return purchaseApplyNo.toString();
    }

    public static void main(String[] args) {
        for (int i=0; i<100; i++){
            PurchaseUtils.producePurchaseApplyNo(1);
        }
    }

}
